package com.zero.basic.dao;

import java.util.Map;

import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

@Repository("sqlQueryBaseDao")
public class SqlQueryBaseDao<T> extends DefaultBaseDao<T> {

	public Object queryBySql(String sql, Object[] args,
			Map<String, Object> alias, Class<?> clz, boolean hasEntity,
			boolean transform) {
		sql = initSort(sql);
		SQLQuery sq = getSession().createSQLQuery(sql);
		setAliasParameter(sq, alias);
		setParameter(sq, args);
		if (transform) {
			if (hasEntity) {
				sq.addEntity(clz);
			} else
				sq.setResultTransformer(Transformers.aliasToBean(clz));
		}
		return sq.uniqueResult();
	}

	public Object queryBySql(String sql, Object[] args, Class<?> clz,
			boolean hasEntity, boolean transform) {
		return this.queryBySql(sql, args, null, clz, hasEntity, transform);
	}

	public Object queryBySql(String sql, Object args, Class<?> clz,
			boolean hasEntity, boolean transform) {
		return this.queryBySql(sql, new Object[] { args }, null, clz,
				hasEntity, transform);
	}

	public Object queryByAlias(String sql, Map<String, Object> alias,
			Class<?> clz, boolean hasEntity, boolean transform) {
		return this.queryBySql(sql, null, alias, clz, hasEntity, transform);
	}
}